﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Text;

using Tradex.ComponentModel;

namespace Tradex.Trading {

    /// <summary>
    /// The Orderkey class identifies a specific order instance.
    /// </summary>
    [Serializable()]
    public sealed class OrderKey : Key<Order> {

        /// <summary>
        /// Public constructor for a new OrderKey.
        /// </summary>
        public OrderKey() {
            Order = Guid.NewGuid();
        }

        /// <summary>
        /// Public constructor fora given OrdeKey.
        /// </summary>
        /// <param name="order"></param>
        public OrderKey(Guid order) {
            Order = order;
        }

        /// <summary>
        /// Gets the Key value.
        /// </summary>
        public Guid Order { get; private set; }

        /// <inheritdoc />
        public override string ToString() {
            return Order.ToString();
        }

        /// <summary>
        /// Transforms an order key to a tag string that is embedded in the orders by the connectors.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ToTagString(OrderKey key) {
            Contract.Assert(key != null);
            if (key == null) {
                throw new ArgumentException("key can not be null", "key");
            }
            return "Tradex:" + key.Order;
        }

        /// <summary>
        /// Parses a Tag string into an OrderKey.
        /// </summary>
        /// <param name="codedOrder"></param>
        /// <returns></returns>
        public static OrderKey ToOrderKey(string codedOrder) {
            if (String.IsNullOrEmpty(codedOrder)) {
                return null;
            }
            if (!codedOrder.StartsWith("Tradex:", StringComparison.OrdinalIgnoreCase)) {
                return null;
            }
            string key = codedOrder.Substring(7);
            return new OrderKey(new Guid (key));
        }

        /// <inheritdoc />
        public override int GetHashCode() {
            return base.GetHashCode();
        }

        /// <inheritdoc />
        public override bool Equals(object obj) {
            OrderKey kObj = obj as OrderKey;
            if (kObj == null) {
                return false;
            }
            return kObj == this;
        }

        /// <inheritdoc />
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062")]
        public static bool operator ==(OrderKey left, OrderKey right) {
            if (Object.ReferenceEquals(left, right)) {
                return true;
            }
            if (Object.ReferenceEquals(right, null)) {
                return false;
            }
            if (left.Order != right.Order) {
                return false;
            }
            return true;
        }

        /// <inheritdoc />
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062")]
        public static bool operator !=(OrderKey left, OrderKey right) {
            if (Object.ReferenceEquals(left, right)) {
                return true;
            }
            if (Object.ReferenceEquals(right, null)) {
                return false;
            }      
            if (left.Order != right.Order) {
                return true;
            }
            return false;
        }

    }
}
